package com.itcast.fjs.controller;

import com.itcast.fjs.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

/**
 * @author feng js
 */
@RestController
@RequestMapping("consumer")
@DefaultProperties(defaultFallback = "defalutfallback")
public class UserController {
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    DiscoveryClient discoveryClient;

    @HystrixCommand(threadPoolProperties ={@HystrixProperty(name = "coreSize",value = "3")} )
    @GetMapping("{id}")
    public String findUser(@PathVariable("id") Integer id) {
//        List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
//        ServiceInstance instance = instances.get(0);
        String url = "http://user-service/user/"+ id;

        String user = restTemplate.getForObject(url, String.class);

        return user;
    }

    public String fallback(Integer id) {
        return "系统错误，请联系管理员";
    }
    public String defalutfallback() {
        return "系统错误，请联系管理员";
    }
}
